<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script type="text/javascript">
    let arr
    // 累加
    arr = [1, 2, 3]
    let total = 0
    for (let i = 0; i < arr.length; i++) {
      total += arr[i]
    }
    console.log(total) // 6

    // 使用 reduce 累加
    arr = [1, 2, 3]
    // reduce 四个参数 分别为累计值、当前元素、当前索引、原数组
    const sum = arr.reduce((acc, current) => acc + current, 0)
    console.log(sum)

    // 使用 reduce 来实现 map 同样的功能
    arr = [1, 2, 3]
    const mapArray = arr.map(value => value * 2)
    const reduceArray = arr.reduce((acc, current) => {
      // acc 是累计的值
      // current 是当前遍历的值
      acc.push(current * 2)
      // 相当于 acc = acc
      // 最后 return 的就是 最终的acc的值
      return acc
    }, [])
    console.log(mapArray, reduceArray) // [2, 4, 6]
  </script>
</body>
</html>